<?php
    class Orgao extends BaseModel
    {
        protected $orgaoid;
        protected $tipoadministracaoid;
        protected $classificacaoid;
        protected $nome;
        protected $sigla;
        protected $endereco;
        protected $numero;
        protected $complemento;
        protected $cep;
        protected $bairro;
        protected $cnpj;
        protected $ie;
        protected $im;
        protected $numerosiof; 
        protected $telefone1;       
        protected $telefone2;

        const tablename = "orgaos";
        const tablekey = "orgaoid";
        const tablesequence = "orgaos_orgaoid_seq";

        
        public function salvar(){
        	$this->classificacaoid = 2;
        	$this->telefone2 = TUtils::FoneToNum( $this->telefone2 );
        	$this->telefone1 = TUtils::FoneToNum( $this->telefone1 );
        	$this->cep = TUtils::CepToNum ($this->cep);
        	parent::salvar();
        }
        
        public function getLotacoes(){
        	$lotacoes = new Lotacao();
        	$criterio = array();
        	$criterio[ 'precondicao' ] = 'orgaoid = '.$this->orgaoid;
        	return $lotacoes->listar($criterio);
        }
        
    public function verificarDependencias( $ids )
        {
            $idsSql = '';
            if (is_array ($ids)){
                foreach ($ids as $id){
                    $idsSql .= ', ' . $id;
                }
                $idsSql = substr( $idsSql, 1 );
            }
            elseif (is_numeric ( $ids)){
                $idsSql = $ids;
            }
            else {
                throw new Exception( 'ID inválido.' );
            }
            
            $contrato = new Contrato();
            $result = $contrato->listar( array('precondicao' => $this->getTableKey() . ' IN (' . $idsSql . ')' ) );
            
            $operador = new Operador();
            $result2 = $operador->listar( array('precondicao' => $this->getTableKey() . ' IN (' . $idsSql . ')' ) );
            
            if (pg_num_rows ($result) > 0 OR pg_num_rows ($result2) > 0){
                if (is_array ($ids)){
                    throw new Exception( TMensagens::getChavesEstrangeiras() );
                }
                else {
                    throw new Exception( TMensagens::getChaveEstrangeira() );
                }
            }
        }
        
        public function excluir()
        {
            if ( $this->getTableKeyValue() ){
                $this->verificarDependencias ($this->getTableKeyValue());
                parent::excluir();
            }            
        }
        
        public function excluirGrupo ($ids)
        {
            $this->verificarDependencias($ids);
            parent::excluirGrupo ($ids);
        }
        
	    /*public function excluirGrupo( $ids )
	    {
	    	$sql = "DELETE FROM " . self::getTableName() . " WHERE " . self::getTableKey() . " in (";
	        for($i = 0; $i < count($ids); $i++)
	        {
	        	$sql .= $ids[$i];
	            if ($i < count($ids)-1)
	            	$sql .= ",";
	            else
	            	$sql .= ")";
	        }
	        pg_query(conexao::Conectar(), $sql) or die ("Erro ao excluir.");
	    }*/
    }
?>